
package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Index;

/**
 * This entity represents instances of given MedicalExaminationTypeEntityImpl for given related PersonEntityImpl.
 * This means that given PersonEntityImpl has passed given MedicalExaminationTypeEntityImpl.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name = "MEDICAL_EXAMINATION")
public class MedicalExaminationEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = -4318181001409286326L;

	/*
	 * Entity attributes
	 */
	@Column(nullable=false)
	@Index(name="IDX_MEDEXAM_LAST_PASS")
	private Date passedTimestamp;

	@ManyToOne(fetch = FetchType.EAGER, targetEntity=MedicalExaminationTypeEntityImpl.class, optional=false) // no cascade
	private MedicalExaminationTypeEntityImpl medicalExaminationType;

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.EAGER, targetEntity = PersonEntityImpl.class, mappedBy = "medicalExaminations") // no cascade
	private Set<PersonEntityImpl> persons;


	public MedicalExaminationEntityImpl()
	{
		super();
	}

	public MedicalExaminationEntityImpl(MedicalExaminationTypeEntityImpl medType, Date passTimestamp)
	{
		super();
		setMedicalExaminationType(medType);
		setPassedTimestamp(passTimestamp);
	}

	public MedicalExaminationTypeEntityImpl getMedicalExaminationType()
	{
		return medicalExaminationType;
	}

	public void setMedicalExaminationType(MedicalExaminationTypeEntityImpl medicalExaminationType)
	{
		this.medicalExaminationType = medicalExaminationType;
	}

	public Date getExpiration()
	{
		// TODO: Compute expiration here from EmployeeEntityImpl => Roles => RoleTypeEntityImpl => 
		return null;
	}
	
	public Date getPassedTimestamp()
	{
		return passedTimestamp;
	}

	public void setPassedTimestamp(Date passedTimestamp)
	{
		this.passedTimestamp = passedTimestamp;
	}

	public Set<PersonEntityImpl> getPersons()
	{
		return persons;
	}

	public List<PersonEntityImpl> getPersonsAsList()
	{
		return new ArrayList<PersonEntityImpl>(getPersons());
	}

    
  public void setPersons(Set<PersonEntityImpl> persons)
  {
    this.persons = persons;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("MedicalExaminationEntityImpl [passedTimestamp=");
		builder.append(passedTimestamp);
		builder.append(", medicalExaminationType=");
		builder.append(medicalExaminationType);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}
	
	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.MedicalExamination#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}